//
//  Wphone.m
//  timelyRain
//
//  Created by Jiafei Wei on 16/7/23.
//  Copyright © 2016年 Jiafei Wei. All rights reserved.
//

#import "Wphone.h"
#import "MBTUiColor.h"
#import "JRSegmentControl.h"
#import "OneVC.h"
#import "TwoVC.h"
#import "ThreeVC.h"

@interface Wphone ()<UIScrollViewDelegate, JRSegmentControlDelegate>
{
    JRSegmentControl *segment;
    BOOL _isDrag;
    BOOL _isDidLayout;
    NSInteger selectIndex;
}
@property (weak, nonatomic) IBOutlet UIView *headView;
@property (weak, nonatomic) IBOutlet UIScrollView *contentView;
@property (weak, nonatomic) IBOutlet UIView *titleView;

@end

@implementation Wphone
- (void)viewDidLoad {
    [super viewDidLoad];
    selectIndex=0;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [_contentView setBackgroundColor:[UIColor yellowColor]];
    [_headView setBackgroundColor:[UIColor colorwithHexString:@"#1186D6"]];
    [_titleView setBackgroundColor:[UIColor colorwithHexString:@"#1186D6"]];
    _titleView.layer.cornerRadius = 15;
    _titleView.layer.masksToBounds = YES;
}


/** 设置segment */
- (void)setupSegmentControl
{
    //设置titleView
    CGRect rr=_titleView.bounds;
    segment = [[JRSegmentControl alloc]initWithFrame:CGRectMake(0, 0, rr.size.width, 30)];
    segment.cornerRadius = 15;
    segment.titleColor = [UIColor whiteColor];
    segment.indicatorViewColor = [UIColor whiteColor];
    segment.backgroundColor = [UIColor colorwithHexString:@"#046EB8"];
    segment.titles = @[@"最近纪录", @"联系人", @"动态"];
    segment.delegate = self;
    [segment setSelectedIndex:selectIndex];
    [_titleView addSubview:segment];
}
- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    NSLog(@"viewDidLayoutSubviews");
}


-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:YES];
    _isDidLayout =true;
    NSLog(@"viewDidAppear");
}
-(void)viewWillLayoutSubviews{//在这里得真正的控件的frame去处理
    [super viewWillLayoutSubviews];
    self.navigationController.navigationBarHidden=YES;
    NSLog(@"viewWillLayoutSubviews");
    if (!_isDidLayout) {
        [self setupSegmentControl];
        [self setupViewControllers];
    }
}

/** 设置子视图控制器，这个方法必须在viewDidLoad方法里执行，否则子视图控制器各项属性为空 */
- (void)setupViewControllers
{
    CGRect rr =_contentView.bounds;
    OneVC *one = [[OneVC alloc]initWithNibName:@"OneVC" bundle:nil];
    [self addChildViewController:one];
    one.view.frame = CGRectMake(0, rr.origin.y, rr.size.width, rr.size.height);
    [_contentView addSubview:one.view];

    TwoVC *two = [[TwoVC alloc]initWithNibName:@"TwoVC" bundle:nil];
    [self addChildViewController:two];
    two.view.frame = CGRectMake(rr.size.width,rr.origin.y, rr.size.width, rr.size.height);
    [_contentView addSubview:two.view];
    
    ThreeVC *three = [[ThreeVC alloc]initWithNibName:@"ThreeVC" bundle:nil];
    [self addChildViewController:three];
    three.view.frame = CGRectMake(rr.size.width *2,rr.origin.y,rr.size.width, rr.size.height);
    [_contentView addSubview:three.view];
    [_contentView setContentSize:CGSizeMake(rr.size.width *3,0)];
    
    _contentView.pagingEnabled = YES;
    _contentView.delegate = self;
}


#pragma mark - UIScrollViewDelegate

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [segment selectedBegan];
    _isDrag = YES;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //NSLog(@"scrollViewDidScroll");
    if (_isDrag) {
        CGFloat percent = scrollView.contentOffset.x / scrollView.contentSize.width;
        [segment setIndicatorViewPercent:percent];
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
     //NSLog(@"scrollViewDidEndDragging");
    if (decelerate == NO) {
        [segment selectedEnd];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    //NSLog(@"scrollViewDidEndDecelerating");
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    [segment setSelectedIndex:index];
    selectIndex =index;
    _isDrag = NO;
}

#pragma mark - JRSegmentControlDelegate

- (void)segmentControl:(JRSegmentControl *)segment didSelectedIndex:(NSInteger)index {
    CGFloat X = index * self.view.frame.size.width;
    CGFloat Y = _contentView.bounds.origin.y;
    [_contentView setContentOffset:CGPointMake(X, Y) animated:YES];
}

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}


@end

